package com.sina.shop.api;

import com.sina.shop.domain.address.Address;
import com.sina.shop.service.address.AddressService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-07-16:02
 */
@RequestMapping("/address")
@RestController
public class AddressController extends BaseController {

    @Autowired
    @Qualifier("addressService")
    private AddressService addressService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getAddressList", method = RequestMethod.GET)
    public List<Address> getAddressList(@RequestParam(value = "adminId") Long adminId) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getAddressList, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return addressService.getAddressListByAdminId(adminId);
    }

    @RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
    public Integer deleteAddress(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteAddress, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return addressService.deleteAddress(id);
    }

    @RequestMapping(value = "/saveAddress", method = RequestMethod.POST)
    public Address saveAddress(Address address) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveAddress, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return addressService.saveAddress(address);
    }

    @RequestMapping(value = "/updateAddress", method = RequestMethod.POST)
    public Address updateAddress(Address address) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateAddress, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return addressService.updateAddress(address);
    }

    @RequestMapping(value = "/updateAddressNot", method = RequestMethod.POST)
    public Integer updateAddressNot(Long aid) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateAddressNot, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return addressService.updateAddressStatusNot(aid);
    }

    @RequestMapping(value = "/updateAddressUse", method = RequestMethod.POST)
    public Integer updateAddressUse(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateAddressUse, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return addressService.updateAddressStatusUse(id);
    }

}
